package com.example.wx.service;

import com.alibaba.fastjson.JSONObject;
import com.example.wx.domain.Account;
import com.example.wx.persistence.AccountDAO;
import com.example.wx.persistence.DAOImpl.AccountDAOImpl;
import com.example.wx.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountService {
    private String msg;

    public Account login(String username,String password){
        Account account = null;
        AccountDAO accountDAO = new AccountDAOImpl();
        try {
            account= accountDAO.findAccountByUsernameAndPassword(username,password);
            msg="登陆成功";
        }catch (Exception e){
            msg="登陆失败，请检测账号或密码输入是否正确";
            e.printStackTrace();
        }
        return account;
    }



    public boolean registerAdmin(Account account){
        int result=-1;
        AccountDAO accountDAO = new AccountDAOImpl();
        result=accountDAO.registerNewAdmin(account);
        if(result!=-1){  //用户不存在，可以注册
            try {
                msg = "注册成功";
            }catch (Exception e){
                msg = "服务器错误，注册失败";
                e.printStackTrace();
            }
        }else {  //用户已经存在，注册失败
            msg = "用户已存在，注册失败";
        }
        return result>0;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public static void main(String[] args) throws Exception {
        Account loginAccount;
        AccountService accountService = new AccountService();
//        loginAccount = accountService.login("admin1","111");
//        if(loginAccount != null){
//            System.out.println(loginAccount.getPhone_num());;
//        }else {
//            System.out.println("登陆失败");
//        }


        Account account = new Account();
        account.setUsername("3");
        account.setUsername("admin3");
        account.setPassword("333");
        account.setTruename("name3");
        account.setId_num("1651231564");
        account.setPhone_num("5165165161654984");
        accountService.registerAdmin(account);
        System.out.println(accountService.getMsg());
    }
}
